@echo off
echo SMS Receiver HTTP Access Setup
echo ===============================
echo.

:: Check for administrator privileges
net session >nul 2>&1
if %errorLevel% == 0 (
    echo [OK] Running with administrator privileges
) else (
    echo [ERROR] This script requires administrator privileges
    echo Please right-click and select "Run as administrator"
    echo.
    pause
    exit /b 1
)

echo.
echo Choose an option:
echo 1. Configure URL reservation for HTTP access via IP address
echo 2. Add Windows Firewall rule for HTTP port
echo 3. Remove URL reservation
echo 4. Remove Windows Firewall rule
echo 5. Check current configuration
echo 6. Exit
echo.
set /p choice="Enter your choice (1-6): "

if "%choice%"=="1" goto :configure_url
if "%choice%"=="2" goto :add_firewall
if "%choice%"=="3" goto :remove_url
if "%choice%"=="4" goto :remove_firewall
if "%choice%"=="5" goto :check_config
if "%choice%"=="6" goto :exit
goto :invalid_choice

:configure_url
echo.
echo Configuring URL reservation for HTTP access...
echo This allows the application to bind to http://*:8889/ without administrator privileges
echo.
netsh http add urlacl url=http://*:8889/ user=Everyone
if %errorLevel% == 0 (
    echo [OK] URL reservation configured successfully
    echo The application can now bind to all network interfaces on port 8889
) else (
    echo [ERROR] Failed to configure URL reservation
)
echo.
pause
goto :menu

:add_firewall
echo.
echo Adding Windows Firewall rule for HTTP port 8889...
echo.
netsh advfirewall firewall add rule name="SMS Receiver HTTP" dir=in action=allow protocol=TCP localport=8889
if %errorLevel% == 0 (
    echo [OK] Firewall rule added successfully
    echo Incoming connections to port 8889 are now allowed
) else (
    echo [ERROR] Failed to add firewall rule
)
echo.
pause
goto :menu

:remove_url
echo.
echo Removing URL reservation...
echo.
netsh http delete urlacl url=http://*:8889/
if %errorLevel% == 0 (
    echo [OK] URL reservation removed successfully
) else (
    echo [WARNING] URL reservation may not exist or failed to remove
)
echo.
pause
goto :menu

:remove_firewall
echo.
echo Removing Windows Firewall rule...
echo.
netsh advfirewall firewall delete rule name="SMS Receiver HTTP"
if %errorLevel% == 0 (
    echo [OK] Firewall rule removed successfully
) else (
    echo [WARNING] Firewall rule may not exist or failed to remove
)
echo.
pause
goto :menu

:check_config
echo.
echo Checking current configuration...
echo.
echo === URL Reservations ===
netsh http show urlacl | findstr ":8889"
if %errorLevel% == 0 (
    echo [Found URL reservations for port 8889]
) else (
    echo [No URL reservations found for port 8889]
)
echo.
echo === Firewall Rules ===
netsh advfirewall firewall show rule name="SMS Receiver HTTP" >nul 2>&1
if %errorLevel% == 0 (
    echo [OK] SMS Receiver HTTP firewall rule exists
    netsh advfirewall firewall show rule name="SMS Receiver HTTP"
) else (
    echo [No SMS Receiver HTTP firewall rule found]
)
echo.
echo === Port Usage ===
netstat -an | findstr ":8889"
if %errorLevel% == 0 (
    echo [Found processes using port 8889]
) else (
    echo [No processes currently using port 8889]
)
echo.
pause
goto :menu

:invalid_choice
echo Invalid choice. Please try again.
echo.
pause

:menu
cls
echo SMS Receiver HTTP Access Setup
echo ===============================
echo.
echo Choose an option:
echo 1. Configure URL reservation for HTTP access via IP address
echo 2. Add Windows Firewall rule for HTTP port
echo 3. Remove URL reservation
echo 4. Remove Windows Firewall rule
echo 5. Check current configuration
echo 6. Exit
echo.
set /p choice="Enter your choice (1-6): "

if "%choice%"=="1" goto :configure_url
if "%choice%"=="2" goto :add_firewall
if "%choice%"=="3" goto :remove_url
if "%choice%"=="4" goto :remove_firewall
if "%choice%"=="5" goto :check_config
if "%choice%"=="6" goto :exit
goto :invalid_choice

:exit
echo.
echo Setup completed. You can now:
echo 1. Start the SMS Receiver application
echo 2. Test HTTP access using: python check_network.py
echo 3. Access via browser: http://localhost:8889/?code=123456
echo 4. Access via IP: http://[your-ip]:8889/?code=123456
echo.
echo Goodbye!
pause
